package com.anhdt.utils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class VUtils {

//    public static String getExceptionStackTrace(Exception e) {
//        if (null == e) {
//            return StringUtils.EMPTY;
//        }
//        StringWriter sw = new StringWriter();
//        e.printStackTrace(new PrintWriter(sw, true));
//        return sw.toString();
//    }
    public static String patentCommon = "yyyy-MM-dd HH:mm:ss.SSS";

    public static String getCurrentTimeStamp() {
        SimpleDateFormat sdfDate = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss.SSS");// dd/MM/yyyy
        Date now = new Date();
        String strDate = sdfDate.format(now);
        return strDate;
    }

    public static String md5(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes());
            BigInteger number = new BigInteger(1, messageDigest);
            String hashtext = number.toString(16);
            while (hashtext.length() < 32) {
                hashtext = "0" + hashtext;
            }
            return hashtext;
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    public static boolean regexCheck(String input, String patent) {
        return input.matches(patent);
    }

    public Date StringToDateWithPattern(String input, String pattern) {
        //String pattern = "dd-MM-yyyy";
        Date date = new Date();
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(pattern);// "dd/MM/yyyy"
            date = formatter.parse(input);

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date;

    }

    public static final String dateToString(Date date, String patten) {
        if (patten == null) {
            patten = VUtils.patentCommon;
        }
        DateFormat df = new SimpleDateFormat(patten);
        String reportDate = df.format(date);
        return reportDate;
    }
    
    public static String stripTag(String input){
            return input.replaceAll("\\<.*?>","");
    }

//   public static String convertMinisecondToTime(long minisecond){
//       final DateTime dt = new DateTime(minisecond);
//       final String result = dt.toString("HH:mm:ss");
//       return result;
//   }
}
